package com.test.leetcode;

/**
 * https://leetcode.cn/problems/maximum-length-of-repeated-subarray/
 * @author sujiafa
 * @date 2025/4/9
 */
public class n718_最长重复子数组 {

    public int findLength(int[] nums1, int[] nums2) {

        // 线性DP知识一例题
        int lengthText1 = nums1.length;
        int lengthText2 = nums2.length;
        int maxLength = 0;
        // dp[i][j]表示text1中前i个元素组成的子数组arr1与text2中前j个元素组成的子数组arr2的最长公共子序列长度为dp[i][j]
        int[][] dp = new int[lengthText1 + 1][lengthText2 + 1];
        for (int i = 1; i < lengthText1 + 1; i++) {
            for (int j = 1; j < lengthText2 + 1; j++) {
                if (nums1[i - 1] == nums2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                    maxLength = Math.max(maxLength, dp[i][j]);
                } else {
                    dp[i][j] = 0;
                }
            }
        }

        return maxLength;
    }
}
